﻿(function () {
	var initTemplate = function (text, load) {
		if (!text)
			text = '';
		var templateFunction = _.template(text);
		load(templateFunction);
	}

	define(['jquery', 'underscore'], function ($, _) {
		return {
			load: function (name, req, load) {
				var parts = name.split('#');

				if (parts.length > 1) { //multiple templates in the file
					var $template = $('#' + parts[1]);
					if ($template.length > 0) {
						initTemplate($template.html(), load);
					}
					else {
						var url = req.toUrl(parts[0]);
						$.get(url, function (data) {
							$('body').append(data);

							var $template = $('#' + parts[1]);
							initTemplate($template.html(), load);
						});
					}
				}				
			}
		}
	});
})();